package com.amat0urheu.controller.adminController;

import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.annotation.OperateLog;
import com.amat0urheu.pojo.CloudPojo.ImagePojo;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.FlavorService;
import com.amat0urheu.service.cloudService.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class ImageController {
    @Autowired
    ImageService imageService;

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/cloud/getAllImages/{pageSize}/{pageNum}")
    public ReturnRes getAllFlavors(@PathVariable int pageSize,
                                   @PathVariable int pageNum) {

        return      imageService.getAllImages(pageSize,pageNum);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/cloud/refreshImages")
    ReturnRes refreshNetwork() {
      imageService.refreshImages();
        return new ReturnRes(true, "刷新成功");
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/cloud/getAllMachines")
    ReturnRes getAllMachines() {

        return   imageService.getAllMachines();
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description="0",action="更新了Image")
    //    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/cloud/imageUpdateSubmit")
    public ReturnRes updateImage(@RequestParam(name = "data") String imageJson) {
        ImagePojo image = JSONObject.parseObject(imageJson, ImagePojo.class);
        return imageService.updateImage(image);
    }
}